package com.motong.gongdan.config.wechat;

import cn.hutool.core.date.DateTime;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.motong.gongdan.util.RedisUtil;
import com.motong.gongdan.util.StringUtil;
import com.motong.gongdan.vo.system.CustomEvent.MissionEventMsg;
import com.motong.gongdan.vo.system.CustomEvent.WorkOrderEventMsg;
import com.motong.gongdan.vo.system.CustomEvent.WorkReportEventMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author:fff
 * @time:2022/5/13
 */
@Component
public class ConfigurationService {
    private String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&";

    private final String redirectUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s?param=%s&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";

    @Autowired
    private WechatConfig wechatConfig;

    public JSONObject getAccessToken() {
        String requestUrl = accessTokenUrl + "appid=" + wechatConfig.getAppid() + "&secret=" + wechatConfig.getKey();
        String resp = HttpUtil.get(requestUrl);
        JSONObject result = JSONUtil.parseObj(resp);
        RedisUtil.set("access_token", result.getStr("access_token"), 7000);
        return result;
    }

    /**
     * 获取用户列表
     *
     * @param accessToken
     * @return
     */

    public JSONObject getUserList(String accessToken) {
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken + "&next_openid=";
        String resp = HttpUtil.get(requestUrl);
        JSONObject result = JSONUtil.parseObj(resp);
        System.out.println("用户列表:" + resp);
        return result;
    }

    public JSONObject sendMsg(String token, String openId) {
        String requestUrl = " https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;
        Map<String, Object> content = new HashMap<>();
        JSONObject data = JSONUtil.createObj();
        data.put("first", new JSONObject().put("value", "工单新增"));
        data.put("keyword1", new JSONObject().put("value", "GD00000001"));
        data.put("keyword2", new JSONObject().put("value", "测试"));
        data.put("keyword3", new JSONObject().put("value", "2022-05-16"));
        data.put("remark", new JSONObject().put("value", "测试"));

        content.put("touser", openId);
        content.put("url", "");
        content.put("template_id", "0bq5dWCeOiyAmL9rqodTmV5gwkCGeqW9PXkW0O3Ss-E");
        content.put("data", data);
        String resp = HttpUtil.post(requestUrl, JSON.toJSONString(content).toString());
        System.out.println(content.toString());
        System.out.println(JSON.toJSONString(content));
        JSONObject result = JSONUtil.parseObj(resp);
        System.out.println("发送消息:" + resp);
        return result;
    }

    public String sendMsgWorkOrder(WorkOrderEventMsg msg, String openId, String token) {
        String requestUrl = " https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;
        Map<String, Object> content = new HashMap<>();
        JSONObject data = JSONUtil.createObj();
        data.put("first", new JSONObject().put("value", StringUtil.isBlank(msg.getTitle()) ? "-" : msg.getTitle()));
        data.put("keyword1", new JSONObject().put("value", StringUtil.isBlank(msg.getWorkOrderCode()) ? "-" : msg.getWorkOrderCode()));
        data.put("keyword2", new JSONObject().put("value", StringUtil.isBlank(msg.getProductName()) ? "-" : msg.getProductName()));
        data.put("keyword3", new JSONObject().put("value", StringUtil.isBlank(msg.getTime()) ? "-" : msg.getTime()));
        data.put("remark", new JSONObject().put("value", StringUtil.isBlank(msg.getComment()) ? "-" : msg.getComment()));

        content.put("touser", openId);
        content.put("url",String.format(redirectUrl, msg.getWorkOrderId() + "," + 1));
        content.put("template_id", "0bq5dWCeOiyAmL9rqodTmV5gwkCGeqW9PXkW0O3Ss-E");
        content.put("data", data);
        String resp = HttpUtil.post(requestUrl, JSON.toJSONString(content).toString());
        System.out.println(content.toString());
        System.out.println(JSON.toJSONString(content));
        JSONObject result = JSONUtil.parseObj(resp);
        System.out.println("发送消息:" + resp);
        return JSON.toJSONString(content);
    }

    public String sendMsgMission(MissionEventMsg msg, String openId) {
        JSONObject accessToken = getAccessToken();
        String token = accessToken.getStr("access_token");
        String requestUrl = " https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;
        Map<String, Object> content = new HashMap<>();
        JSONObject data = JSONUtil.createObj();
        data.put("first", new JSONObject().put("value", msg.getTitle()));
        data.put("keyword1", new JSONObject().put("value", msg.getWorkOrderCode()));
        data.put("keyword2", new JSONObject().put("value", msg.getProcessName()));
        data.put("keyword3", new JSONObject().put("value", msg.getText()));
        data.put("keyword4", new JSONObject().put("value", msg.getUser()));
        data.put("remark", new JSONObject().put("value", msg.getMsg()));

        content.put("touser", openId);
        content.put("url", String.format(redirectUrl, msg.getMissionId() + "," + 2));
        content.put("template_id", "Cpx2vel8jIePN6PzMpt4p_2xnbBn3UcYE4B9JAMzwu0");
        content.put("data", data);
        String resp = HttpUtil.post(requestUrl, JSON.toJSONString(content).toString());
        System.out.println(content);
        System.out.println(JSON.toJSONString(content));
        JSONObject result = JSONUtil.parseObj(resp);
        System.out.println("发送消息:" + resp);
        return JSON.toJSONString(content);
    }

    public String sendMsgReport(WorkReportEventMsg msg, String openId) {
        JSONObject accessToken = getAccessToken();
        String token = accessToken.getStr("access_token");
        String requestUrl = " https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;
        Map<String, Object> content = new HashMap<>();
        JSONObject data = JSONUtil.createObj();
        data.put("first", new JSONObject().put("value", msg.getTitle()));
        data.put("keyword1", new JSONObject().put("value", msg.getProcessCode()));
        data.put("keyword2", new JSONObject().put("value", msg.getProductName()));
        data.put("keyword3", new JSONObject().put("value", StringUtils.hasLength(msg.getTime()) ? msg.getTime() : DateTime.now()));
        data.put("remark", new JSONObject().put("value", msg.getMsg()));

        content.put("touser", openId);
        content.put("url",String.format(redirectUrl, msg.getWorkReportId() + "," + 3));
        content.put("template_id", "S83zAYwig3_AbF37ePGKyFjTofQ_KsiBkWexDPro75M");
        content.put("data", data);
        String resp = HttpUtil.post(requestUrl, JSON.toJSONString(content).toString());
        System.out.println(content);
        System.out.println(JSON.toJSONString(content));
        JSONObject result = JSONUtil.parseObj(resp);
        System.out.println("发送消息:" + resp);
        return JSON.toJSONString(content);
    }
}
